<?php
/**
 * Created by PhpStorm.
 * User: 94004
 * Date: 2017/12/29
 * Time: 9:36
 */
header("Content-Type:text/html;charset=utf-8;");
require_once "../util/db_helper.php";
$currentPage = $_POST["currentPage"];
$pageCount = $_POST["pageCount"];
$category_id;
$status;
if (isset($_POST["category_id"])) {
    $category_id = $_POST["category_id"];
}
if (isset($_POST["status"])) {
    $status = $_POST["status"];
}
$sql = " SELECT a.id,a.title,u.nickname,c.name,a.created,a.status,a.user_id,a.category_id FROM articles a LEFT JOIN users u ON a.user_id = u.id LEFT JOIN categories c ON a.category_id = c.id ";
$condition = "";
if (isset($category_id) || isset($status)) {
    $condition .= " WHERE ";
}
if (isset($category_id)) {
    $condition .= " a.category_id = {$category_id} ";
}
if (isset($status)) {
    if (isset($category_id)) {
        $condition .= " AND a.status = {$status} ";
    }
    else {
        $condition .= " a.status = {$status} ";
    }
}
$offset = ($currentPage - 1) * $pageCount;
$sql = $sql . $condition . " LIMIT {$offset},{$pageCount} ";
$res = query($sql);
$countSql = " SELECT COUNT(*) AS dataCount FROM articles a " . $condition;
$countRes = query($countSql);
$arr = array("code" => 101, "msg" => "操作失败");
if ($res) {
    $arr["code"] = 100;
    $arr["msg"] = "操作成功";
    $arr["data"] = $res;
    $arr["dataCount"] = $countRes[0]["dataCount"];
}
echo json_encode($arr, JSON_UNESCAPED_UNICODE);
?>